<?php
class HttpContext{
	const REQUEST_GET = "GET";
	const REQUEST_POST = "POST";
	const SCRIPT_NAME = "index.php";
	const URL_TYPE_QUERY = "QUERY_STRING";
	const URL_TYPE_URI = "REQUEST_URI";
	const HTTP_PORT = 80;
	const HTTPS_PORT = 443;
	private $_serverUrl;
	private $_baseUrl;
	private $_requestUri;
	private $_redirectUrl;
	private $_requestUrl;
	private $_requestString;
	private $_queryString;
	private $_parram;
	private $_RouteRequest = array();
	private $_Request = array();
	
	
	public function __construct(){
		$this->_init();
	}
	
	private function _init(){
		$this->_Request = $_REQUEST;
		$port = $_SERVER['SERVER_PORT'];
		$this->_queryString = $_SERVER['QUERY_STRING'];
		if($port == HttpContext::HTTP_PORT || $port == HttpContext::HTTPS_PORT){
			$port = STRING_EMPTY;
		}
		else{
			$port = ":".$port;
		}
		$requestScheme = (isset($_SERVER['REQUEST_SCHEME']) && ($_SERVER['REQUEST_SCHEME'] == "http" ||$_SERVER['REQUEST_SCHEME'] == "https" ))?$_SERVER['REQUEST_SCHEME']:"http";
		$this->_serverUrl = $requestScheme."://".$_SERVER['SERVER_NAME'].$port;
		$documentFolder = str_replace(HttpContext::SCRIPT_NAME, STRING_EMPTY, $_SERVER['SCRIPT_NAME']);
		$this->_baseUrl = $this->_serverUrl . $documentFolder;
		$this->_requestUri = $_SERVER['REQUEST_URI'];
		$this->_parram = explode(SLASH, substr($this->_requestUri, 1));
		$this->_requestUrl = $this->_serverUrl . $this->_requestUri;
		$this->_requestString = substr($_SERVER['REQUEST_URI'], (strlen($documentFolder) - 1));
		$redirectUrl = "";
		if(isset($_SERVER['REDIRECT_URL']))$redirectUrl = $_SERVER['REDIRECT_URL'];
		$this->_redirectUrl = substr($redirectUrl, (strlen($documentFolder) - 1));
	}
	
	public function fileRequest($name,$default = null){
		if(isset($_FILES[$name])){
			return $_FILES[$name]; 
		}
		else{
			return $default;
		}
	}
	public function request($name,$default = null){
		if(isset($this->_Request[$name])){
			return $this->_Request[$name]; 
		}
		else{
			return $default;
		}
	}
	
	public function postRequest($name,$default = null){
		if(isset($_POST[$name])){
			return $_POST[$name]; 
		}
		else{
			return $default;
		}
	}
	
	public function getRequest($name,$default = null){
		if(isset($_GET[$name])){
			return $_GET[$name]; 
		}
		else{
			return $default;
		}
	}

	public function fileRequests(){
		return $_FILES;
	}
	public function requests(){
		return $this->_Request;
	}
	
	public function postRequests(){
		return $_POST;
	}
	
	public function getRequests(){
		return $_GET;
	}
	
	public function getServerUrl(){
		return $this->_serverUrl;
	}
	public function getBaseUrl(){
		return $this->_baseUrl;
	}

	public function getQueryString(){
		return $this->_queryString;
	}
	public function getParram(){
		return $this->_parram;
	}
	public function getRequestUri(){
		return $this->_requestUri;
	}
	public function getRequestUrl(){
		return $this->_requestUrl;
	}

	public function getRequestString(){
		return $this->_requestString;
	}
	public function getRedirectUrl(){
		return $this->_redirectUrl;
	}
	public function addRouteRequest($key, $value){
		$this->_Request[$key] = $value;
	} 
	/**
	 * Check request is post or get.
	 * @return true or false
	 */
	public function isPost(){
		return ($_SERVER['REQUEST_METHOD'] == HttpContext::REQUEST_POST) ? true : false; 
	}
}